﻿using Microsoft.AspNetCore.Mvc;
using NewsEntity.Models;
using NewsSevicer.Interface;
using NewsSevicer.ResultClass;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace NewsWebsite.Controllers
{
    public class NewsController : Controller
    {
        public INews _news;
        public NewsController(INews news)
        {
            _news = news;
        
        }

        public IActionResult Index()
        {
          List<NewsType> list = _news.GetNewsType();

           ViewBag.NewsType = list;
            return View();
            
        }
        /// <summary>
        /// 获取新闻
        /// </summary>
        /// <returns></returns>
        public ResultData GetNews() 
        {
            string Url = GetCompleteUrl(); 
              object list=  _news.GetNews(Url);

            return new ResultData {code=200,message="新闻获取成功",data=list };
        
        
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="Typeid"></param>
        /// <returns></returns>
        [HttpPost]
        public string GetNewsType([FromBody] NewsType news) {

            return  _news.GetNewsType(news.TypeId);
        }

        public ResultData GetPLNews() 
         {
            string Url = GetCompleteUrl();
            ResultData data =      _news.GetPLNews(Url);
        return data;
        
        }

        /// <summary>
        /// //获取当前项目的地址
        /// </summary>
        /// <returns></returns>
        public   string GetCompleteUrl()
        {
            return new StringBuilder()
                 .Append(HttpContext.Request.Scheme)
                 .Append("://")
                 .Append(HttpContext.Request.Host)
                 .Append(HttpContext.Request.PathBase)
                 //.Append(HttpContext.Request.Path)
                 //.Append(HttpContext.Request.QueryString)
                 .ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ResultData GetBannerInfo()
        {
            string url = GetCompleteUrl();
            var bannerManager = _news.GetBannerInfo(url);

            return new ResultData
            {
                code = 200,
                message = "请求成功",
                data = bannerManager

            };
        }



        public IActionResult NewsList() 
        {
            List<NewsType> list = _news.GetNewsType();
            ViewBag.NewsType = list;
            return View();
        
        
        }

        public IActionResult NewsShow() 
        {


            return View();
        }

        /// <summary>
        /// 评论存储
        /// </summary>
        /// <param name="newInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public int NewsComment([FromBody] NewInfo newInfo) 
        {
            if (newInfo == null) {

                return 66666;
            }
            newInfo.CreateTime = DateTime.Parse(DateTime.Now.ToString("f"));
                if (_news.NewsComment(newInfo))
                {
                    return 1;
                }
                return 0;
          
         
        
        }
        /// <summary>
        /// 获取所有评论
        /// </summary>
        /// <param name="NewsId"></param>
        /// <returns></returns>
       [HttpPost]
        public ResultData GETNewsComment([FromBody] NewInfo newInfo) { 
        
            ResultData resultData =   _news.GETNewsComment(newInfo.NewsId);

            return resultData;
        
        }


        //[HttpPost]

        //public ResultData GetSingleNews([FromBody] News news) 
        //{
        //    ResultData data = new ResultData();
        //    var result=   _news.GetSingleNews(news.Id);

        //    if (!string.IsNullOrEmpty(result.Image)&& result!=null)
        //    {
        //        // 使用Split方法将字符串按逗号分割成子字符串数组  
        //        string[] parts = result.Image.Split(',');

        //        // 将数组中的每个元素添加到List<string>中  

        //        result.Image = GetCompleteUrl() + parts[0];
        //        data.message = "成功获取到新闻！"; 
        //        data.code = 200;
        //        data.data = result;
        //      return data; 
        //    }
        //    data.message = "没有拿到该新闻！";
        //    data.code = 201;
        //    data.data = null;

        //    return data ;
        //}

        [HttpPost]
        public ResultData GetSingleNews([FromBody] News news)
        {
            ResultData data = new ResultData();

            // 检查 news.Id 是否为空
            if (news == null || news.Id == 0)
            {
                data.message = "新闻ID不能为空！";
                data.code = 400;
                data.data = null;
                return data;
            }

            var result = _news.GetSingleNews(news.Id);

            // 检查 result 是否为空
            if (result == null)
            {
                data.message = "没有找到该新闻！";
                data.code = 404;
                data.data = null;
                return data;
            }

            // 处理图片URL
            if (!string.IsNullOrEmpty(result.Image))
            {
                // 使用Split方法将字符串按逗号分割成子字符串数组
                string[] parts = result.Image.Split(',');

                // 将数组中的第一个元素添加到完整URL中
                result.Image = GetCompleteUrl() + parts[0];
            }

            data.message = "成功获取到新闻！";
            data.code = 200;
            data.data = result;

            return data;
        }


        /// <summary>
        /// 按照新闻类别查询新闻
        /// </summary>
        /// <param name="id">新闻类别id</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData GetTypeNews([FromBody] News news)
        {

            if (news==null)
            {
                return new ResultData
                {
                    code = 201,
                    message = "分类参数为空",
                    data = null
                };
            }

            List<News> list;
            string url;
            try
            {
               url= GetCompleteUrl();
              list = _news.GetTypeNews(news.Id);
            }
            catch (Exception)
            {
                return new ResultData
                {
                    code = 500,
                    message = "系统异常！",
                    data = null
                };
            }

            if(list.Count > 0)
            {
                return new ResultData
                {
                    code = 200,
                    message =url,
                    data = list
                };
            }
            return new ResultData
            {
                code = 500,
                message = "查询成功，没有任何博客文章",
                data = list
            };
        }
        
    }
}
